"use strict";
const axios = require("axios");
const path = require("path");
const userHome = require("user-home");
const { readFile } = require("@wenjun-cli/utils");
// 自定义常量
const DEFAULT_CLI_HOME = ".wenjun-cli";
const LOGIN_TOKEN_FILE = ".login_token";
const BASE_URL = process.env.WENJUN_CLI_BASE_URL;
const tokenFilePath = path.resolve(
  userHome,
  DEFAULT_CLI_HOME,
  LOGIN_TOKEN_FILE
);
const request = axios.create({
  baseURL: BASE_URL,
  timeout: 5000,
  headers: {
    authorization: `Bearer ${readFile(tokenFilePath)}`,
  },
});
request.interceptors.response.use(
  (response) => {
    if (response.data.code === -1) {
      throw new Error(response.data.message);
    }
    return response.data;
  },
  (error) => Promise.reject(error)
);
module.exports = request;
